home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / PasswordView.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  127 lines

  1. /*
  2.  * @(#)PasswordView.java    1.3 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text;
  21.  
  22. import java.awt.*;
  23. import com.sun.java.swing.JPasswordField;
  24.  
  25. /**
  26.  * Implements a View suitable for use in JPasswordField 
  27.  * UI implementations.  This is basically a field ui that
  28.  * renders its contents as the echo character specified
  29.  * in the associated component (if it can narrow the 
  30.  * component to a JPasswordField).
  31.  *
  32.  * @author  Timothy Prinzing
  33.  * @version 1.3 04/09/98
  34.  * @see     View
  35.  */
  36. public class PasswordView extends FieldView {
  37.  
  38.     /**
  39.      * Constructs a new view wrapped on an element.
  40.      *
  41.      * @param elem the element
  42.      */
  43.     public PasswordView(Element elem) {
  44.     super(elem);
  45.     }
  46.  
  47.     /**
  48.      * Renders the given range in the model as normal unselected
  49.      * text.  This sets the foreground color and echos the characters
  50.      * using the value returned by getEchoChar().
  51.      *
  52.      * @param g the graphics context
  53.      * @param x the starting X coordinate >= 0
  54.      * @param y the starting Y coordinate >= 0
  55.      * @param p0 the starting offset in the model >= 0
  56.      * @param p1 the ending offset in the model >= p0
  57.      * @returns the X location of the end of the range >= 0
  58.      * @exception BadLocationException if p0 or p1 are out of range
  59.      */
  60.     protected int drawUnselectedText(Graphics g, int x, int y, 
  61.                      int p0, int p1) throws BadLocationException {
  62.                      
  63.     Container c = getContainer();
  64.     if (c instanceof JPasswordField) {
  65.         JPasswordField f = (JPasswordField) c;
  66.         g.setColor(f.getForeground());
  67.         char echoChar = f.getEchoChar();
  68.         int n = p1 - p0;
  69.         for (int i = 0; i < n; i++) {
  70.         x = drawEchoCharacter(g, x, y, echoChar);
  71.         }
  72.     }
  73.     return x;
  74.     }
  75.  
  76.     /**
  77.      * Renders the given range in the model as selected text.  This
  78.      * is implemented to render the text in the color specified in
  79.      * the hosting component.  It assumes the highlighter will render
  80.      * the selected background.  Uses the result of getEchoChar() to
  81.      * display the characters.
  82.      *
  83.      * @param g the graphics context
  84.      * @param x the starting X coordinate >= 0
  85.      * @param y the starting Y coordinate >= 0
  86.      * @param p0 the starting offset in the model >= 0
  87.      * @param p1 the ending offset in the model >= p0
  88.      * @returns the X location of the end of the range >= 0.
  89.      * @exception BadLocationException if p0 or p1 are out of range
  90.      */
  91.     protected int drawSelectedText(Graphics g, int x, 
  92.                    int y, int p0, int p1) throws BadLocationException {
  93.     g.setColor(selected);
  94.     Container c = getContainer();
  95.     if (c instanceof JPasswordField) {
  96.         JPasswordField f = (JPasswordField) c;
  97.         g.setColor(f.getSelectedTextColor());
  98.         char echoChar = f.getEchoChar();
  99.         int n = p1 - p0;
  100.         for (int i = 0; i < n; i++) {
  101.         x = drawEchoCharacter(g, x, y, echoChar);
  102.         }
  103.     }
  104.     return x;
  105.     }
  106.  
  107.     /**
  108.      * Renders the echo character, or whatever graphic should be used 
  109.      * to display the password characters.  The color in the Graphics
  110.      * object is set to the appropriate foreground color for selected
  111.      * or unselected text.
  112.      *
  113.      * @param g the graphics context
  114.      * @param x the starting X coordinate >= 0
  115.      * @param y the starting Y coordinate >= 0
  116.      * @param c the echo character
  117.      * @return the updated X position >= 0
  118.      */
  119.     protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
  120.     ONE[0] = c;
  121.     g.drawChars(ONE, 0, 1, x, y);
  122.     return x + g.getFontMetrics().charWidth(c);
  123.     }
  124.  
  125.     static char[] ONE = new char[1];
  126. }
  127.